<template>
	<view class="topic-detail-item">
		<scroll-view class="scroll-box" scroll-y="true" scroll-with-animation enable-back-to-top>
			<!--用户信息-->
			<view class="topic-item-user u-flex u-p-30">
				<u-avatar :src="topicInfo.avatar" size="mini"></u-avatar>
				<text class="nickname u-margin-left-20">{{topicInfo.nickname}}</text>
				<!--关注用户按钮-->
				<view class="follow-btn" @tap="onFollow()">
				    <u-tag text="已关注" v-if="topicInfo.isAttention =='1'"></u-tag>
					<u-tag text="关注" v-if="topicInfo.isAttention =='0'"></u-tag>
				</view>
			</view>
			<!--帖子内容-->
			<view class="topic-detail-content u-padding-30">
				<u-parse :html="topicInfo.text" :lazy-load="true"></u-parse>
				<!--1张图-->
				<view v-if="topicInfo.images.length == 1">
					<image :lazy-load="true" mode="aspectFill" :src="topicInfo.images[0]" class="img-style-1"
						@tap.stop="previewImage(topicInfo.images[0],topicInfo.images)">
					</image>
				</view>
				<!--2张图-->
				<view v-if="topicInfo.images.length == 2">
					<view class="img-style-2">
						<image :lazy-load="true" v-for="(mediaItem, index2) in topicInfo.images" :key="index2"
							mode="aspectFill" :src="mediaItem" @tap.stop="previewImage(mediaItem,topicInfo.images)">
						</image>
					</view>

				</view>
				<!--3张以上-->
				<view v-if="topicInfo.images.length > 2">
					<view class="img-style-3">
						<image :lazy-load="true" v-for="(mediaItem, index2) in topicInfo.images" :key="index2"
							mode="aspectFill" :src="mediaItem" @tap.stop="previewImage(mediaItem,topicInfo.images)">
						</image>
					</view>
				</view>
			</view>
			<!--评论区-->
			<u-gap height="20" bg-color="#f4f6f8"></u-gap>
			<view class="u-bg-white u-p-30">
				<view class="u-p-b-10 u-tips-color">发表评论</view>
				<view class="">
					<u-input v-model="inputText" type="textarea" :placeholder="placeholder" :border="false" />
				</view>
				<view class="u-flex u-row-center">

					<u-button hover-class="none" type="primary" size="medium" @click="sendBtn()">
						立即评论
					</u-button>
				</view>
			</view>
			<!--评论列表-->
			<view class="comment-area u-p-30 u-bg-white">
				<view class="u-p-b-10 u-tips-color">评论列表</view>
				<view class="comment" v-for="(item, index) in comment.list" :key="index">
					<view class="left">
						<u-avatar :src="item.author.avatar" size="60"></u-avatar>
					</view>
					<view class="right">
						<view class="top">
							<view class="u-light-color">
								<text class="u-p-l-10 name u-line-1">{{item.author.nickname }}</text>
								<text class="u-m-l-30">{{ item.create_date }}</text>
							</view>
							<view class="replay" @click="replyComment(index)">
								<u-icon name="chat" :size="30"></u-icon>
								<view class="opeate">回复</view>
							</view>
						</view>
						<view class="content">
							<rich-text :nodes="item.text"></rich-text>
						</view>
						<!--评论回复列表-->
						<view class="comment-reply" v-for="(items, indexs) in item.children" :key="indexs">
							<view class="comment-reply-info u-tips-color">
								<text class="">{{items.author.nickname}}</text>
								<text class="text-bold" style="margin: 0 4rpx;"> 回复</text>
								<text class="">{{items.reply.nickname}} </text>
							</view>
							<view class="comment-reply-content u-m-t-10">
								{{items.text}}
							</view>
						</view>
					</view>
				</view>
				<view class="" v-if="!comment.list.length">
					<u-empty text="暂无评论"></u-empty>
				</view>
			</view>
		</scroll-view>
	</view>

</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				topicInfo: [],
				inputText: "",
				comment: {
					more: 'load',
					total: 0,
					page: 1,
					list: [],
				},
				placeholder: "留下自己的评论吧",
				replyParams: {
					id: 0,
					reply: 0,
					text: '',
				},
				showLoad: true,
				isfollow: false,

			}
		},
		onLoad() {
			this.getTopicDetail();
			this.initComment();
		},
		computed: {

		},

		methods: {
			//帖子详情
			getTopicDetail() {
				let that = this;
				return new Promise((resolve, reject) => {
					that.$http(
						'topic.topicdetail', {
							id: that.$Route.query.id
						},
						'',
						false
					).then(res => {
						if (res.code == 1) {
							that.topicInfo = res.data;
							resolve(res.data);
						} else {
							resolve(false);
						}
					});
				});
			},
			sendComment() {
				this.$http('topic.topicReview', {
					text: this.inputText,
					id: this.topicInfo.id
				}).then(res => {
					if (res.code) {
						this.$u.toast(res.msg);
						this.inputText = '';
						this.initComment();
					}
				});
			},
			// 初始化评论
			initComment() {
				this.$http('topic.topicReviewlist', {
					id: this.$Route.query.id
				}).then(res => {
					if (res.code == 1) {
						this.comment.list = res.data.list;
						this.comment.page++;
						this.comment.total = res.data.total;
						this.comment.more = res.data.more;
						this.placeholder = '留下自己的评论吧'
					}
				});
			},
			//发送评论
			sendBtn() {
				if (this.$u.test.rangeLength(this.inputText, [3, 500])) {
					if (this.replyParams.id) {
						this.$u.throttle(this.sendReply(), 500);
					} else {
						this.$u.throttle(this.sendComment(), 500);
					}
				} else {
					this.$u.toast('评论字数限制3-500字');
				}
			},
			//回复评论
			replyComment(index, indexs = 0) {
				this.inputText = '';
				let author = indexs ? this.comment.list[index].children[indexs].author : this.comment.list[index].author;
				this.replyParams.reply = author.id;
				this.replyParams.id = this.comment.list[index].id;
				this.placeholder = '回复' + author.nickname + '的评论';

			},
			sendReply() {
				this.replyParams.text = this.inputText;
				this.$http('topic.topicReply', this.replyParams).then(res => {
					if (res.code == 1) {
						this.$u.toast(res.msg);
						this.inputText = '';
						this.replyParams.id = 0;
						this.initComment();
					}
				});
			},
			//关注用户
			
			onFollow() {
				this.$http('topic.topicFollow', {
					attention_id: this.topicInfo.user_id,
					discover_id: this.topicInfo.id
				}).then(res => {
					if (res.code == 1) {
						this.isfollow = !this.isfollow;
						this.topicInfo.follow = res.data;
						this.$u.toast(res.msg);
					}
				});
			}

		}
	}
</script>

<style lang="scss" scoped>
	.topic-detail-item {
		width: 100%;
		height: 100vh;
		line-height: 1.5em;
		background: #FFFFFF;
		margin-bottom: 10rpx;
		overflow: hidden;
		padding-bottom: 20rpx;

		.topic-detail-content {
			margin-top: 20rpx;
			margin-bottom: 20rpx;

			.img-style-1 {
				display: block;
				width: 100%;
				max-height: 600rpx;
				border-radius: 5px;
				overflow: hidden;
				margin-top: 20rpx;
			}

			.img-style-2 {
				display: flex;

				image {
					margin: 5rpx;
					border-radius: 5px;
					width: 100%;
					height: 294rpx;
				}
			}

			.img-style-3 {
				display: flex;
				flex-wrap: wrap;

				image {
					width: 31.3%;
					height: 200rpx;
					margin: 1%;
					border-radius: 5px;
				}

			}

		}
	}

	.follow-btn {
		position: absolute;
		right: 30rpx;
	}

	.msg-fad {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		padding: 20rpx;
		display: flex;

		input {
			flex: 1;
			height: 80rpx;
			background-color: #F5F5F5;
			padding: 0 20rpx;
			font-size: 24rpx;
			margin-right: 20rpx;
			border-radius: 10rpx;
		}
	}

	.u-content {
		margin-top: 20rpx;
	}

	.comment {
		background-color: #ffffff;
		display: flex;
		padding: 30rpx;

		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}

		.right {
			flex: 1;
			padding-left: 20rpx;

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.replay {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;

					.opeate {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}

				.name {
					max-width: 100rpx;
				}
			}

			.content {
				margin-bottom: 10rpx;
				word-break: break-word;
			}
		}
	}
</style>
